package cn.elwy.rcp.ui.explorer.editor;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;

import cn.elwy.common.util.AssertUtil;
import cn.elwy.rcp.ui.explorer.core.PathConst;
import cn.elwy.rcp.ui.explorer.core.ProjectConst;
import cn.elwy.rcp.ui.explorer.core.vo.BuilderModule;
import cn.elwy.rcp.ui.explorer.core.vo.ProjectConfig;
import cn.elwy.rcp.ui.table.TablePane;
import cn.elwy.rcp.ui.table.TablePaneFactory;
import cn.elwy.rcp.ui.wizard.BaseWizardPage;

public class ModuleChoiceEditorPage extends BaseWizardPage {

	public static final String ID = "ModuleChoiceEditorPage";

	private Composite container;
	private TablePane tablePane;
	private Text mvnCmdText;

	private ProjectConfig projectConfig;
	private Text warPathText;

	private Button warButton;

	private Button incrementWarButton;

	private Button jarButton;

	/**
	 * Create the wizard.
	 */
	public ModuleChoiceEditorPage() {
		super(ID);
		setTitle("选择编译模块");
		setDescription("选择需要重新编译打包的模块");
	}

	/**
	 * Create contents of the wizard.
	 * @param parent
	 */
	public void createControl(Composite parent) {

		container = new Composite(parent, SWT.NULL);
		GridLayout layout = new GridLayout();
		container.setLayout(layout);

		setControl(container);

		Composite composite = new Composite(container, SWT.NONE);
		composite.setLayout(new GridLayout(3, false));
		composite.setLayoutData(new GridData(SWT.FILL, SWT.TOP, true, false, 1, 1));

		Label lblwar = new Label(composite, SWT.NONE);
		lblwar.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false, 1, 1));
		lblwar.setText("基线WAR包路径：");

		warPathText = new Text(composite, SWT.BORDER);
		warPathText.setMessage("WAR包绝对路径");
		warPathText.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1));

		Composite composite_1 = new Composite(composite, SWT.NONE);
		composite_1.setLayout(new RowLayout(SWT.HORIZONTAL));
		composite_1.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));

		warButton = new Button(composite_1, SWT.RADIO);
		warButton.setText("WAR包");

		incrementWarButton = new Button(composite_1, SWT.RADIO);
		incrementWarButton.setText("增量WAR包");

		jarButton = new Button(composite_1, SWT.RADIO);
		jarButton.setText("JAR包");

		Label lblNewLabel = new Label(composite, SWT.NONE);
		lblNewLabel.setText("Maven执行命令：");

		mvnCmdText = new Text(composite, SWT.BORDER);
		mvnCmdText.setMessage("Maven 打包命令");
		GridData gd_mvnCmdText = new GridData(GridData.FILL_HORIZONTAL);
		gd_mvnCmdText.horizontalSpan = 2;
		mvnCmdText.setLayoutData(gd_mvnCmdText);
		// Composite tablePane = new Composite(container, SWT.NONE);
		tablePane = TablePaneFactory.getTablePane(container, "cn.elwy.rcp.ui.explorer.ModuleChoice");
		GridData layoutData = new GridData(SWT.FILL, SWT.FILL, true, true, 1, 1);
		layoutData.heightHint = 1;
		tablePane.setLayoutData(layoutData);

		addListener();
		init();
	}

	private void addListener() {
	}

	private void init() {
		PackerEditorWizard packerEditorWizard = (PackerEditorWizard) getWizard();
		projectConfig = packerEditorWizard.getProjectConfig();
		ModuleChoiceModel moduleChoiceModel = packerEditorWizard.getModuleChoiceModel();
		tablePane.setInputModel(moduleChoiceModel);

		String warPath = projectConfig.getWarPath();
		if (AssertUtil.isNotEmpty(warPath)) {
			warPathText.setText(warPath);
		}
		String mvnCmd = projectConfig.getMvnCmd();
		if (AssertUtil.isEmpty(mvnCmd)) {
			mvnCmd = PathConst.BUILDER_COMMAND;
		}
		mvnCmdText.setText(mvnCmd);
		int packType = projectConfig.getPackType();
		if (packType == ProjectConst.PACK_TYPE_WAR) {
			warButton.setSelection(true);
		} else if (packType == ProjectConst.PACK_TYPE_INCREMENT_WAR) {
			incrementWarButton.setSelection(true);
		} else {
			jarButton.setSelection(true);
		}
	}

	@Override
	public void reset() {
		tablePane.refresh();
	}

	// @Override
	// public boolean nextButtonPressed() {
	// ModuleChoiceModel moduleChoiceModel = ((PackerEditorWizard) getWizard()).getModuleChoiceModel();
	// List<BuilderModule> inputModel = moduleChoiceModel.getInput();
	// for (BuilderModule builderModule : inputModel) {
	// if (builderModule.isBuilder()) {
	// return true;
	// }
	// }
	// return false;
	// }

	public Object getPageData() {
		List<BuilderModule> moduleList = tablePane.getInputModel().getInput();
		projectConfig.setModuleList(moduleList);

		projectConfig.setWarPath(warPathText.getText());
		projectConfig.setMvnCmd(mvnCmdText.getText());

		int packType = ProjectConst.PACK_TYPE_WAR;
		if (incrementWarButton.getSelection()) {
			packType = ProjectConst.PACK_TYPE_INCREMENT_WAR;
		} else if (jarButton.getSelection()) {
			packType = ProjectConst.PACK_TYPE_JAR;
		}
		projectConfig.setPackType(packType);
		return projectConfig;
	}

	@Override
	public boolean finishButtonPressed() {
		return nextButtonPressed();
		// IWizard wizard2 = getWizard();
		// PackerEditorWizard newProjectWizard = (PackerEditorWizard) wizard2;
		// ProjectConfig pc = newProjectWizard.getNewProjectModel().getInput();
		// PackerService s = AppContext.getService(PackerService.ID, PackerService.class);
		// s.run(pc);

		// pc.setName(projectNameText.getText());
		// pc.setVersion(projectVersionText.getText());
		// pc.setProjectPath(projectPathText.getText());
		// pc.setResourcePath(resourcePathText.getText());
		// pc.setOutputPath(outputPathText.getText());
		// pc.setScmUrl(scmUrlText.getText());
		// pc.setScmUser(scm);
		// pc.setScmPassword(scmPassword);
		// pc.setStartVersion(startVersion);
		// pc.setEndVersion(endVersion);
		// pc.setSourceDirectory(sourceDirectory);
		// pc.setDocDirectory(docDirectory);
		// pc.setDemoDirectory(demoDirectory);
		// pc.setDbScriptDirectory(dbScriptDirectory);
		// pc.setContainerDirectory(containerDirectory);
		// pc.setParentPom(parentPom);
		//
		// ConfigurationNode rootNode = config.getRootNode();
		// List<ConfigurationNode> plans = rootNode.getChildren(PLANS2);
		// int planCount = plans.get(0).getChildrenCount(PLAN);
		// List<String> planList = new ArrayList<String>();
		// for (int i = 0; i < planCount; i++) {
		// String fileName = config.getString(PLANS_PLAN + i + FILE_NAME);
		// planList.add(fileName);
		// }
		// pc.setPlanList(planList);
		//
		// {
		// FtpInfo release = new FtpInfo();
		// String location = config.getString(RELEASE_LOCATION);
		// String ip = config.getString(RELEASE_IP);
		// int port = config.getInt(RELEASE_PORT, 0);
		// String userName = config.getString(RELEASE_USER_NAME);
		// String password = config.getString(RELEASE_PASSWORD);
		// release.setLocation(location);
		// release.setIp(ip);
		// release.setPort(port);
		// release.setUserName(userName);
		// release.setPassword(password);
		// pc.setRelease(release);
		// }
		//
		// List<ConfigurationNode> deploys = rootNode.getChildren(DEPLOYS2);
		// int deployCount = deploys.get(0).getChildrenCount(DEPLOY);
		// List<FtpInfo> deployList = new ArrayList<FtpInfo>();
		// for (int i = 0; i < deployCount; i++) {
		// FtpInfo ftpInfo = new FtpInfo();
		// String ip = config.getString(DEPLOYS_DEPLOY + i + IP2);
		// int port = config.getInt(DEPLOYS_DEPLOY + i + PORT2, 0);
		// String userName = config.getString(DEPLOYS_DEPLOY + i + USER_NAME);
		// String password = config.getString(DEPLOYS_DEPLOY + i + PASSWORD2);
		// ftpInfo.setIp(ip);
		// ftpInfo.setPort(port);
		// ftpInfo.setUserName(userName);
		// ftpInfo.setPassword(password);
		// ftpInfo.setName("");
		// deployList.add(ftpInfo);
		// }
		// pc.setDeployList(deployList);
		// dialogSettings.put("projectName", projectNameText.getText());
		// try {
		// dialogSettings.save("C:/temp/demo.txt");
		// } catch (IOException e) {
		// e.printStackTrace();
		// }
		// return super.finishButtonPressed();
	}
}
